package com.punai.config.controller;

import cn.hutool.core.collection.CollUtil;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.TreeBo;
import com.punai.config.constants.PnConfigConstants;
import com.punai.config.domain.ConfigInventoryChapter;
import com.punai.config.domain.EngineerLinkTemplate;
import com.punai.config.service.ConfigEngineerClassService;
import com.punai.config.service.ConfigGenerateCodeService;
import com.punai.config.sqlitedomain.ConfigEngineerClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/config/engineerClass")
public class ConfigEngineerClassController extends BaseController {

    @Autowired
    private ConfigEngineerClassService configEngineerClassService;
    @Autowired
    private ConfigGenerateCodeService configGenerateCodeService;

    @PreAuthorize("@ss.hasPermi('config:engineerClass:list')")
    @GetMapping("/treelist")
    public AjaxResult treelist()
    {
        List<ConfigEngineerClass> list = configEngineerClassService.treelist();
        return AjaxResult.success(list);
    }


    @PreAuthorize("@ss.hasPermi('config:engineerClass:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        ConfigEngineerClass engineerClass = configEngineerClassService.getInfo(id);
        if (engineerClass!=null){
            List<EngineerLinkTemplate> linkTemplateEngineer = configEngineerClassService.getLinkTemplateEngineer(engineerClass.getCode());
            List<String> collect = linkTemplateEngineer.stream().map(EngineerLinkTemplate::getTemplateId).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(collect)) engineerClass.setTemplateIdArr(collect);
        }
        return AjaxResult.success(engineerClass);
    }


    @PreAuthorize("@ss.hasPermi('config:engineerClass:add')")
    @PostMapping
    public AjaxResult add(@RequestBody ConfigEngineerClass configEngineerClass)
    {
        configEngineerClassService.addOrUpdate(configEngineerClass);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('config:engineerClass:edit')")
    @PutMapping
    public AjaxResult update(@RequestBody ConfigEngineerClass configEngineerClass)
    {
        configEngineerClassService.addOrUpdate(configEngineerClass);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('config:engineerClass:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        configEngineerClassService.delete(id);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('config:engineerClass:query')")
    @GetMapping("getEngNextCode")
    public AjaxResult getEngNextCode() {
        String nextCode = configGenerateCodeService.getNextCode(PnConfigConstants.CODE_TYPE.ENG_CODE);
        return AjaxResult.success("成功",nextCode);
    }


}
